var store = {
	save(key,value){
		localStorage.setItem(key,JSON.stringify(value));
	},
	fetch(key){
		return JSON.parse(localStorage.getItem(key)) || [];
	}
}
var initData={
	sort:store.fetch("sort")||0	//当前选择的任务类型
}
var runTimeHandle=null;
var list =[];//任务列表
var userId=store.fetch("userId");
var vm = new Vue({
	el: ".main",
	data: {
		taskList:[],
		departList:[],
		curDepart:"",
		curUserId:"",
		userList:[]
	},
	created:function(){
		axios({
		    method: 'get',
		    url:"/depart/list",
		}).then((res)=>{
			this.departList=res.data.data;
		})
	},
	watch:{

	},
	computed:{
		unCheckedLength(){
			return  this.filter(1,false).length
		},
		finishedPlIST(){	
			return this.filter(0,true);
		},
		unfinishedPlIST(){//未完成的计划
			return this.filter(0,false);
		},
		finishedAlIST(){//完成的临时任务				
			return this.filter(1,true);
		},
		unfinishedAlIST(){//未完成的临时任务				
			return this.filter(1,false);
		},
		okrList(){
			return this.filter(2,false);
		}
		
	},
	methods: {
		selDepart(departNo){//改变任务类型
			this.curDepart=departNo;
			axios.get("/user/list?departNo="+this.curDepart).then((res)=>{
				this.userList=res.data.data;
			 }).catch((error)=>{
				 this.loginTip=error;
			 });
		},
		selUser(userId){//改变任务类型
			if(this.curUserId==userId){
				this.curUserId="";
			}else{
				this.curUserId=userId;
				axios.get("/task/list/"+this.curUserId).then((res)=>{
					this.taskList=res.data.data;
				 }).catch((error)=>{
					 alert("系统异常："+error)
				 });	
			}
			
		},
		filter(sort,checked){
			return this.taskList.filter(function(item) {
				return item.sort==sort&&item.checked==checked;
			});
		},
		addTodo(ev){
			var now=new Date();
			if(this.task !== ""){
				var item = {
					taskId:userId+new Date().getTime(),
					task:this.task,
					checked:false,
					sort:this.sort,
					startTime:now.format("hh:mm"),
					endTime:(now.getHours()+2)+":"+now.getMinutes(),
					status:0,//创建任务 0 暂停执行
					lastSaveRuntime:0,//上一次保存的运行时间
					timeList:[],
					createDate:now.format("yyyy-MM-dd"),
					userId:this.userId
				}
				this.list.push(item)
			this.task = "";
			}
		},
		deleteTodo(item){
			var index = this.list.indexOf(item);
			this.list.splice(index,1);
		},
		editTodo(item){	
			this.beforeTitle = item.task;
			this.editItem = item;
		},
		edited(item){
			this.editItem = ""
		},
		cancel(item){
			item.task =  this.beforeTitle;
			this.editItem = "";
			this.beforeTitle = ""
		},
		opTask(item){
			clearInterval(runTimeHandle)
			this.allPause();
			item.status++;
			if(item.status>1){
				item.status=0;
			}	
			if(item.status==1){
				this.startTask(item)
			}
		},
		startTask(item){
			item.startTime=Date.now();
			this.runItem=item;
			clearInterval(runTimeHandle);
			this.run.time=item.lastSaveRuntime;
			runTimeHandle=setInterval(this.addRunTime,1000)
		},
		continueTask(){
			clearInterval(runTimeHandle);
			runTimeHandle=setInterval(this.addRunTime,1000)
		},
		showTime(second,item){
			if(item){
				second=item.lastSaveRuntime+parseInt((Date.now()-item.startTime)/1000);
			}
			if(second<60){
				return second; 
			}
			var min=parseInt(second/60)　;
			var sec=second % 60;
			if(min<60){
				return min+":"+(sec<10?"0"+sec:sec);
			}
			var hour=parseInt(min/60)　;
			var min=min%60;
			return hour+":"+(min<10?"0"+min:min)+":"+(sec<10?"0"+sec:sec);
		},
		login(){
			this.loginTip="";
			if(!this.user.name||!this.user.pwd){
				this.loginTip="请输入用户名或密码";
				return ;
			}
			if(!this.user.departNo){
				this.loginTip="请选择部门";
				return ;
			}
			axios.post("/user/login",this.user).then((res)=>{
				var data=res.data;
				if(data.status==0){
					this.userId=res.data.data.userId
				}else{
					this.loginTip=data.msg;
				}
			 }).catch((error)=>{
				 this.loginTip=error;
			 });
		}
	},
	directives:{
		"focus":{									
			update(el,binding){
				if(binding.value){
					el.focus()
				}

			}
		}
	}
});

function watchHashChange(){
	var hash = window.location.hash.slice(1);
	vm.visibility = hash;
};

watchHashChange();

window.addEventListener("hashchange",watchHashChange)
if(userId.length>0){
	axios({
	    method: 'get',
	    url:"/task/list/"+userId,
	    params: {
	    	userId:userId,
	    }
	}).then((res)=>{
		vm.list=res.data.data;
	})	
}

